<?php
namespace app\admin\controller;
use think\Controller;

use app\admin\model\User as UserModel;
use think\Session;

class User extends AdminBase{
    //用户列表页面
    public function index(){
       
        $User = new UserModel();
        
        //$data = $payList->where('userid', $UserID)->select();
        $d = input('get.');
        if (isset($d['key'])) {
            $key = $d['key'];
        }else{
            $key = '';
        }
        $where = array();
        if ($key != '') {
            $where['username'] = array('like','%'.$key.'%');
            $data = $User->where($where)->select();
            $page = '';
            $total = count($data);
            $count = count($data);
        }else{
            $data = $User->paginate(10);
             $page = $data->render();
             $total = $data->total();
             $count = $data->count();
        }
         //dump($data);die;
        
        $this->assign('data',$data);
        

        $this->assign('key',$key);
        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('total',$total);
        $this->assign('count',$count);
        return $this->fetch();
    }
    //用户注册页面展示
    public function register(){

        return $this->fetch();
    }
    //接收注册页面表单
    public function registerForm(){
        $Data = input('post.');

        //存入数据库
        $User = new UserModel();
        $User->data($Data);

        if($User->save()){
            //插入成功
            // echo "插入成功";
        }else{
            //插入失败
            // echo "插入失败";
        }

    }
    //删除数据函数 ajax
    public function deleteUser(){

        $AllData = input('post.');
        //$AllData = '3,hao,2018-03-29 17:01:27,3333333,0,11111111,0';

        $User = new UserModel();
       
        //从$data中得到名字字符串，并且转成数组
         $UserDeleteList = explode(',', $AllData['userinfo_str']);
         //dump($UserNameList);
         
         $SuccessArray = [];
         $FailArray = [];

        foreach ($UserDeleteList as $key => $value) {
            if($User->where(['id' => $value])->find()){
                //从数据库能找到,就删除该条数据
                if($User->where(['id' => $value])->delete()){

                    $SuccessArray[]= $value;
                    // echo "删除成功";
                }else{
                    $FailArray[]= $value;
                    // echo "删除失败";
                }
            }else{
                $FailArray[]= $value;
                //echo "没有本条数据";
            }
        }

        //返回的数组
        $StatusArray = [];
        $StatusArray['status'] = "1";
        $StatusArray['success'] = $SuccessArray ? $SuccessArray : NULL;
        //$StatusArray['success'] = ['aa','bb'];
        $StatusArray['fail'] = $FailArray ? $FailArray : NULL;

        //var_dump($StatusArray);die;
        return json_encode($StatusArray);
        
    }
    //修改数据函数 ajax
    public function modifyUser(){ 

        $AllData = input('post.');
       

        //$AllData = '2,hao,2018-03-29 17:01:27,3333333,0,11111111,0';

       
        //file_put_contents("bb.txt",$AllData['userinfo_str'],FILE_APPEND);
        
        $User = new UserModel();
        $UserModifyList = explode(',', $AllData['userinfo_str']);
        //dump($UserModifyList);die;
        
        $SuccessArray = [];
        $FailArray = [];

        //看看存在吗
        if($User->where(['id' => $UserModifyList['0']])->find()){
            //存在就更新
            $Res = $User->save(
                [
                    'username'  => $UserModifyList['1'],
                    'create_time'  => $UserModifyList['2'],
                    'phone' => $UserModifyList['3'],
                    'memberlevel' => $UserModifyList['4'],
                    'identitynum' => $UserModifyList['5'],
                    'email' => $UserModifyList['6']],
                ['id' => $UserModifyList['0']]);
                //给出成功标志
                $SuccessArray[] = 1;
                if($Res){
                    // echo "111";
                }else{
                    // echo '222';
                }
        }else{
            //不存在直接返回
            $FailArray []= 1;
            // echo  'false';
        }
        //echo $result['id'];die;
        //返回的数组
        $StatusArray = [];
        $StatusArray['status'] = "1";
        $StatusArray['success'] = $SuccessArray ? $SuccessArray : NULL;
        //$StatusArray['success'] = ['aa','bb'];
        $StatusArray['fail'] = $FailArray ? $FailArray : NULL;

        // dump($StatusArray);die;
        //var_dump($StatusArray);die;
        return json_encode($StatusArray);
    }

}